package com.nova.bluetooth.helpUtils;

import android.media.AudioFormat;
import android.util.Pair;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class WavFileInfoReader {
    public static Pair<Integer, Integer> getChannelFormatAndSampleRate(String wavFilePath) {
        int channelCount = 0;
        int sampleRate = 0;

        try {
            FileInputStream fileInputStream = new FileInputStream(new File(wavFilePath));
            byte[] buffer = new byte[4];

            // Seek to the position where the channel count information is stored (usually bytes 22-23).
            fileInputStream.skip(22);

            // Read the 2 bytes representing the channel count (little-endian format).
            fileInputStream.read(buffer, 0, 2);

            // Convert the bytes to a short.
            channelCount = ByteBuffer.wrap(buffer, 0, 2).order(ByteOrder.LITTLE_ENDIAN).getShort();


            // Read the 4 bytes representing the sample rate (little-endian format).
            fileInputStream.read(buffer, 0, 4);

            // Convert the bytes to an integer.
            sampleRate = ByteBuffer.wrap(buffer, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();

            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Pair.create(channelCount, sampleRate);
    }


    public static int getChannelFormat(String wavFilePath) {
        int channelCount = 0;

        try {
            FileInputStream fileInputStream = new FileInputStream(new File(wavFilePath));
            byte[] buffer = new byte[2];

            // Seek to the position where the channel count information is stored (usually bytes
            // 22-23).
            fileInputStream.skip(22);

            // Read the 2 bytes representing the channel count (little-endian format).
            fileInputStream.read(buffer);

            // Convert the bytes to a short.
            channelCount = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getShort();

            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return channelCount;
    }

    public static int getSampleRate(String wavFilePath) {
        int sampleRate = 0;

        try {
            FileInputStream fileInputStream = new FileInputStream(new File(wavFilePath));
            byte[] buffer = new byte[4];

            // Seek to the position where the sample rate information is stored (usually bytes
            // 24-27).
            fileInputStream.skip(24);

            // Read the 4 bytes representing the sample rate (little-endian format).
            fileInputStream.read(buffer);

            // Convert the bytes to an integer.
            sampleRate = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getInt();

            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sampleRate;
    }

    public static int mapChannelFormatToAudioFormat(int channelCount) {
        if (channelCount == 1) {
            return AudioFormat.CHANNEL_IN_MONO;
        } else if (channelCount == 2) {
            return AudioFormat.CHANNEL_IN_STEREO;
        } else {
            return AudioFormat.CHANNEL_IN_MONO; // Default to mono for unknown channel count.
        }
    }
}
